/**
 * @author Loic Bar
 */

(function(){
	powerTodo.UI = {};
	
	powerTodo.UI.createBasedLayout = function()
	{
		var tabGroup = Titanium.UI.createTabGroup();
		
		var todoListWindow = powerTodo.UI.createTodoListWindow();
		var urgentTodoListWindow = powerTodo.UI.createUrgentTododListWindow();
		var lateTodoListWindow = powerTodo.UI.createLateTododListWindow();
		var createTodoWindow = powerTodo.UI.createAddTodoWindow();
		
		var tab1 = Ti.UI.createTab({
			window: todoListWindow,
			title: 'Toutes'
		});
		
		todoListWindow.activity = {
			onCreateOptionsMenu : function(e) {
				var menu = e.menu;
				var m1 = menu.add({ title : 'Nouveau' });
				m1.addEventListener('click', function(e) {
					tab1.open(createTodoWindow);
				});
			}
		};
		
		var tab2 = Ti.UI.createTab({
			window: urgentTodoListWindow,
			title: 'Urgentes'
		});
		
		urgentTodoListWindow.activity = {
			onCreateOptionsMenu : function(e) {
				var menu = e.menu;
				var m1 = menu.add({ title : 'Nouveau' });
				m1.addEventListener('click', function(e) {
					tab2.open(createTodoWindow);
				});
			}
		};
		
		var tab3 = Ti.UI.createTab({
			window: lateTodoListWindow,
			title: 'En retard'
		});
		
		lateTodoListWindow.activity = {
			onCreateOptionsMenu : function(e) {
				var menu = e.menu;
				var m1 = menu.add({ title : 'Nouveau' });
				m1.addEventListener('click', function(e) {
					tab3.open(createTodoWindow);
				});
			}
		};
		
		tabGroup.addTab(tab1);
		tabGroup.addTab(tab2);
		tabGroup.addTab(tab3);
		
		return tabGroup;
	};
	
	powerTodo.UI.createTodoListWindow = function()
	{
		var win = Ti.UI.createWindow({
			title:'Toutes mes tches',
			backgroundColor:'#fff'
		});
		
		var table = Ti.UI.createTableView();
		win.add(table);
		
		if (Ti.Platform.osname === 'iphone') {
			var b = Titanium.UI.createButton({
				title:'Nouveau',
				style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
			});
			b.addEventListener('click',function() {
				winAddContact.open({modal:true});
			});
			win.setRightNavButton(b);
		}
		
		return win;
	};
	
	powerTodo.UI.createUrgentTododListWindow = function()
	{
		var win = Ti.UI.createWindow({
			title:'Tches urgentes',
			backgroundColor:'#fff'
		});
		
		var table = Ti.UI.createTableView();
		win.add(table);
		
		if (Ti.Platform.osname === 'iphone') {
			var b = Titanium.UI.createButton({
				title:'Nouveau',
				style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
			});
			b.addEventListener('click',function() {
				winAddContact.open({modal:true});
			});
			win.setRightNavButton(b);
		}
		
		return win;
	};
	
	powerTodo.UI.createLateTododListWindow = function()
	{
		var win = Ti.UI.createWindow({
			title:'Tches en retard',
			backgroundColor:'#fff'
		});
		
		var table = Ti.UI.createTableView();
		win.add(table);
		
		if (Ti.Platform.osname === 'iphone') {
			var b = Titanium.UI.createButton({
				title:'Nouveau',
				style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
			});
			b.addEventListener('click',function() {
				winAddContact.open({modal:true});
			});
			win.setRightNavButton(b);
		}
		
		return win;
	};
	
	powerTodo.UI.createAddTodoWindow = function()
	{
		var win = Ti.UI.createWindow({
			title:'Ajouter une nouvelle tche',
			backgroundColor:'#fff',
			layout:'vertical'
		});
		
		var tbName = Ti.UI.createTextField({
			hintText:'Indiquez le nom de votre tche',
			top:10,
			left:10,
			width:250,
		});
		
		win.add(tbName);
		
		var labelForDatePicker = Ti.UI.createLabel({
			text:"Date d'chance",
			top:10,
			left:10,
			width:200,
		});
		
		win.add(labelForDatePicker);
		
		var minDate = new Date();
		minDate.setFullYear(2011);
		minDate.setMonth(0);
		minDate.setDate(1);
		
		var maxDate = new Date();
		maxDate.setFullYear(2011);
		maxDate.setMonth(11);
		maxDate.setDate(31);
		
		var value = new Date();
		
		var picker = Ti.UI.createPicker({
		    type: Ti.UI.PICKER_TYPE_DATE,
		    minDate: minDate,
		    maxDate: maxDate,
		    value: value,
		    top:10,
		    left:10,
		});

		// par defaut false
		picker.selectionIndicator = true;
		
		win.add(picker);
		
		var addButton = Ti.UI.createButton({
			title:'Ajouter',
			top: 10,
			left: 10,
			width:200
		});
		
		addButton.addEventListener('click', function(e){
			Ti.API.info('Le bouton a t cliqu');
			// ajouter ici une logique d'ajout dans une base de donnes
		});
		
		win.add(addButton);
		
		if (Ti.Platform.osname === 'iphone') {
			var b = Titanium.UI.createButton({
				title:'Fermer',
				style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
			});
			b.addEventListener('click',function() {
				win.close();
			});
			win.setRightNavButton(b);
		}
		
		return win;
	};
	
	powerTodo.UI.createTodoDetailWindow = function(/* id */ todoId)
	{
		var win = Ti.UI.createWindow({
			title: 'Detail',
			backgroundColor:'#fff'
		});
		
		return win;
	};
	
	powerTodo.UI.createEditTodoWindow = function(/* id */ todoId)
	{
		var win = Ti.UI.createWindow({
			title:'Editer',
			backgroundColor:'#fff'
		});
		
		return win;
	};	
	
})()
